package com.lightkits.wms.controller;

import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.model.CheckResult;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.domain.retirelib.QueryRetireParams;
import com.lightkits.wms.domain.retirelib.UseRetireParams;
import com.lightkits.wms.dto.retirelib.RetireLibRequestType;
import com.lightkits.wms.dto.retirelib.RetireLibResponseType;
import com.lightkits.wms.dto.retirelib.RetireListRequestType;
import com.lightkits.wms.dto.retirelib.RetireListResponseType;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jing.wei@hand-china.com
 * @date 2019/6/17 19:38
 **/
@RestController
@RequestMapping("/RetireLibService")
public class RetireLibController {
    private final UseRetireParams useRetireParams;

    @Autowired
    public RetireLibController(UseRetireParams useRetireParams, QueryRetireParams queryRetireParams) {
        this.useRetireParams = useRetireParams;
        this.queryRetireParams = queryRetireParams;
    }

    @PostMapping(value = "/retireLibScrap")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/retireLibScrap")
    @ResponseBody
    public RetireLibResponseType retireLibScrap(@RequestBody(required = false) RetireLibRequestType request) {
        RetireLibResponseType response = new RetireLibResponseType();
        response.setSuccessful(true);
        try {
            CheckResult<String> result = useRetireParams.retireLib(request.getWorkerId(), request.getOrgId(), request.getMoveTypeId(),request.getRetireLibDtos());
            if (!result.isSuccessful()) {
                return result.fill(response);
            }
            response.setRequestHeaderId(result.getData());
        } catch (LightKitsBizException e) {
            response.setSuccessful(false);
            String mes = StringUtils.substringAfterLast(e.getMessage(), ":");
            response.setMessage(ObjectUtil.isNullOrEmpty(mes) ? e.getMessage() : mes);

        } catch (Exception e) {
            e.printStackTrace();
            response.setSuccessful(false);
            response.setMessage("Retire Libary Faild");
        }
        return response;
    }

    private final QueryRetireParams queryRetireParams;

    @PostMapping(value = "/getReireList")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/getReireList")
    @ResponseBody
    public RetireListResponseType getReireList(@RequestBody(required = false) RetireListRequestType request) {
        RetireListResponseType response = new RetireListResponseType();
        response.setSuccessful(true);
        try {
            response = queryRetireParams.getRetireList(request.getRequestHeaderId());
        } catch (LightKitsBizException e) {
            response.setSuccessful(false);
            String mes = StringUtils.substringAfterLast(e.getMessage(), ":");
            response.setMessage(ObjectUtil.isNullOrEmpty(mes) ? e.getMessage() : mes);

        } catch (Exception e) {
            e.printStackTrace();
            response.setSuccessful(false);
            response.setMessage("Get Retire List Faild");
        }
        return response;
    }


}
